<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="8">
            <el-form-item label="关键字">
              <el-input v-model="searchObj.username" style="width: 95%;" placeholder="用户" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="操作时间">
              <el-date-picker
                v-model="createTimes"
                type="datetimerange"
                range-separator="至"
                start-placeholde="开始时间"
                end-placeholde="结束时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                style="margin-right: 10px; width: 100%;"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData()">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
        </el-row>
      </el-form>
    </div>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%; margin-top: 10px;"
    >

      <el-table-column
        label="序号"
        width="70"
        align="center"
      >
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="username" label="用户名" align="center" />
      <el-table-column prop="createTime" label="创建时间" align="center" />
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />

  </div>
</template>

<script>
// 引入定义接口的js文件
import api from '@/api/loginLog/loginLog'

export default {
  // 定义初始值
  data() {
    return {
      listLoading: false,
      list: null, // 用户列表
      total: 0, // 总记录数
      page: 1, // 当前页
      limit: 10, // 每页显示记录数
      searchObj: {}, // 条件查询封装对象
      username: '',
      createTimes: []
    }
  },
  // 在页面渲染之前执行
  created() {
    this.fetchData()
    this.resetData()
  },
  methods: { // 具体方法
    // 列表
    fetchData(pageNum = 1) {
      // 页数赋值
      this.page = pageNum
      if (this.createTimes && this.createTimes.length === 2) {
        this.searchObj.createTimeBegin = this.createTimes[0]
        this.searchObj.createTimeEnd = this.createTimes[1]
      }
      // ajax
      api.getPageList(this.page, this.limit, this.searchObj)
        .then(response => {
          // console.log(response)
          // 每页数据列表
          this.list = response.data.records
          // 总记录数
          this.total = response.data.total
          this.listLoading = false
        })
    },
    // 重置表单
    resetData() {
      // 清空表单
      this.searchObj = {}
      this.createTimes = []
      // 查询所有数据
      this.fetchData()
      this.listLoading = true
    }
  }
}
</script>

<style>
  .search-div {
    padding: 10px; border: 1px solid #EBEEF5; border-radius: 3px;
  }
  .tools-div {
    margin-top: 10px; padding: 10px; border: 1px solid #EBEEF5; border-radius: 3px;
  }
</style>
